local jiansu = fk.CreateSkill{
    name = "os_heg__jiansu",
    tags = { Skill.DeputyPlace },
}

jiansu:addEffect(fk.AfterCardsMove,{
    mute = true,
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(jiansu.name) then return false end
        if player.phase ~= Player.NotActive then return end
         local cards = {}
        for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(player:getCardIds("h"), info.cardId)  then
              table.insertIfNeed(cards, info.cardId)
            end
          end
          event:setCostData(self,{cards = cards})
          return #cards >0
        end
      end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        return room:askToSkillInvoke(player, {skill_name = jiansu.name})
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        player:broadcastSkillInvoke(jiansu.name)
        local cards = event:getCostData(self).cards
          room:notifySkillInvoked(player, jiansu.name, "special")
          for _, c in ipairs(cards) do
            room:setCardMark(Fk:getCardById(c), "@@os_heg__jiansu-inhand", 1)
          end
          player:showCards(cards)
      end,
})

jiansu:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(jiansu.name) then return false end
        if player == target and player.phase == Player.Play then
          local n = #table.filter(player:getCardIds("h"), function (cid)
            return Fk:getCardById(cid):getMark("@@os_heg__jiansu-inhand") > 0
          end)
          return table.find(player.room.alive_players, function (p)
            return p.hp <= n
          end)
        end
     end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
          local success, dat = room:askToUseActiveSkill(player, {
            skill_name = "#os_heg__jiansu_active",
            prompt = "#os_heg__jiansu-ask",
            cancelable = true,
            extra_data = {skipUse = true},
        })
        if success and dat then
            event:setCostData(self, {cards = dat.cards, targets = dat.targets})
            return true
          end
       end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        player:broadcastSkillInvoke(jiansu.name)
        room:notifySkillInvoked(player, jiansu.name, "support", event:getCostData(self).targets)
        room:doIndicate(player.id, event:getCostData(self).targets)
        Fk.skills["#os_heg__jiansu_active"]:onUse(room, {
            from = player,
            cards = event:getCostData(self).cards,
            tos = event:getCostData(self).targets,
          })
      end,
})

jiansu:addEffect("visibility",{
    card_visible = function(self, player, card)
        if card and Fk:currentRoom():getCardArea(card) == Card.PlayerHand then
          local owner = Fk:currentRoom():getCardOwner(card)
          if owner and card:getMark("@@os_heg__jiansu-inhand") > 0 then
            return true
          end
        end
      end
})

Fk:loadTranslationTable{
    ["os_heg__jiansu"] = "俭素",
    [":os_heg__jiansu"] = "副将技，此武将牌上单独的阴阳鱼个数-1。当你于回合外获得牌后，你可以明置这些牌。出牌阶段开始时，你可以弃置任意张因此技能明置的手牌，令一名体力值不大于X的角色回复1点体力（X为你以此法弃置的牌数）。",

    ["@@os_heg__jiansu-inhand"] = "俭素",
    ["#os_heg__jiansu-ask"] = "俭素：你可弃置任意张俭素牌并令一名体力值不小于弃置牌数的角色回复体力",

    ["$os_heg__jiansu1"] = "扬师北疆，剪覆胡奴！",
    ["$os_heg__jiansu2"] = "陈兵百万，慑敌心胆！",
}

return jiansu